package com.mhc.haval.service.impl;

import com.mhc.haval.constant.AreaEnum;
import com.mhc.haval.constant.IsDeletedEnum;
import com.mhc.haval.dal.model.*;
import com.mhc.haval.dal.query.DeptBizAreaQuery;
import com.mhc.haval.dal.query.StaffAreaQuery;
import com.mhc.haval.manager.*;
import com.mhc.haval.security.dto.DeptAreaDTO;
import com.mhc.haval.security.dto.FuncDTO;
import com.mhc.haval.security.dto.RoleDTO;
import com.mhc.haval.security.dto.UserDTO;
import com.mhc.haval.security.service.IStaffService;
import com.subaru.common.util.BeanCopierUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

/**
 * @author Churry
 * @create 2017-08-30 17:56
 **/
@RestController
public class IStaffServiceImpl implements IStaffService {

    @Autowired
    StaffManager staffManager;
    @Autowired
    RoleManager roleManager;
    @Autowired
    FuncManager funcManager;
    @Autowired
    DepartmentManager departmentManager;
    @Autowired
    StaffAreaManager staffAreaManager;
    @Autowired
    DeptBizAreaManager deptBizAreaManager;

    /**
     * 查询登录用户信息，可供外部调用
     * @param staffPhone
     * @return
     */
    @Override
    public UserDTO getUserByPhone(String staffPhone) {
        Optional<String> staffPhoneOptional = Optional.ofNullable(staffPhone);
        if (staffPhoneOptional.isPresent()) {
            StaffDO staffDO = staffManager.selectByStaffPhone(staffPhone);
            UserDTO user = BeanCopierUtil.convert(staffDO, UserDTO.class);
            //查询员工是否是银行驻点，如果是则设置银行id
            StaffAreaQuery staffAreaQuery = new StaffAreaQuery();
            staffAreaQuery.createCriteria().andStaffIdEqualTo(user.getStaffId()).andAreaTypeEqualTo(AreaEnum.BANK.getCode());
            List<StaffAreaDO> staffAreaDOList = staffAreaManager.selectByQuery(staffAreaQuery);
            if (!staffAreaDOList.isEmpty()) {
                user.setBankId(staffAreaDOList.get(0).getAreaId());
            }
            //获取部门名称与公司编号
            DepartmentDO departmentDO = departmentManager.selectByPrimaryKey(user.getDeptId());
            user.setDeptName(departmentDO.getDeptName());
            user.setCompanyNo(departmentDO.getCompanyNo());
            //获取部门所对应的省份信息
            DeptBizAreaQuery deptBizAreaQuery = new DeptBizAreaQuery();
            deptBizAreaQuery.createCriteria().andDeptIdEqualTo(user.getDeptId()).andIsDeletedEqualTo(IsDeletedEnum.NORMAL.getCode());
            List<DeptBizAreaDO> deptBizAreaDOList = deptBizAreaManager.selectByQuery(deptBizAreaQuery);
            List<DeptAreaDTO> deptBizAreaDTOList = new ArrayList<>();
            for(DeptBizAreaDO deptBizAreaDO : deptBizAreaDOList){
                DeptAreaDTO deptAreaDTO = BeanCopierUtil.convert(deptBizAreaDO, DeptAreaDTO.class);
                deptBizAreaDTOList.add(deptAreaDTO);
            }
            user.setDeptBizAreaList(deptBizAreaDTOList);

            //角色与权限信息
            List<RoleDTO> roleDTOS = new ArrayList<>();
            List<FuncDTO> funcDTOS = new ArrayList<>();
            //获取用户拥有的角色
            for (RoleDO roleDO : roleManager.selectByStaffId(user.getStaffId())) {
                RoleDTO role = BeanCopierUtil.convert(roleDO, RoleDTO.class);
                roleDTOS.add(role);
            }
            user.setRoleList(roleDTOS);
            //获取用户拥有的权限
            for (FuncDO funcDO : funcManager.selectByStaffId(user.getStaffId())) {
                FuncDTO func = BeanCopierUtil.convert(funcDO, FuncDTO.class);
                funcDTOS.add(func);
            }
            user.setFuncList(funcDTOS);
            return user;
        } else {
            throw new UsernameNotFoundException("Staff phone cannot be null!");
        }
    }

    /**
     * 通过id查询员工信息，不包含角色与权限信息，供外部调用
     * @param staffId
     * @return
     */
    @Override
    public UserDTO getUserByStaffId(Long staffId) {
        Optional<Long> staffIdOptional = Optional.ofNullable(staffId);
        if (staffIdOptional.isPresent()) {
            StaffDO staffDO = staffManager.selectByPrimaryKey(staffId);
            UserDTO user = BeanCopierUtil.convert(staffDO, UserDTO.class);
            //查询员工是否是银行驻点，如果是则设置银行id
            StaffAreaQuery staffAreaQuery = new StaffAreaQuery();
            staffAreaQuery.createCriteria().andStaffIdEqualTo(user.getStaffId()).andAreaTypeEqualTo(AreaEnum.BANK.getCode());
            List<StaffAreaDO> staffAreaDOList = staffAreaManager.selectByQuery(staffAreaQuery);
            if (!staffAreaDOList.isEmpty()) {
                user.setBankId(staffAreaDOList.get(0).getAreaId());
            }
            //获取部门名称与公司编号
            DepartmentDO departmentDO = departmentManager.selectByPrimaryKey(user.getDeptId());
            user.setDeptName(departmentDO.getDeptName());
            user.setCompanyNo(departmentDO.getCompanyNo());
            return user;
        } else {
            throw new UsernameNotFoundException("Staff ID cannot be null!");
        }
    }
}
